<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../static/layui/css/layui.css">
    <script src="../static/layui/layui.js"></script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>\Manage users</title>
</head>
<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="layui-logo "><i class="layui-icon layui-icon-cart"></i>Inventory Management</div>
            <ul class="layui-nav layui-layout-left">
                <li class="layui-nav-item"><a href="/index/"><i class="layui-icon layui-icon-table"></i>Goods Management</a></li>
                <li class="layui-nav-item"><a href="/count/"><i class="layui-icon layui-icon-rmb"></i>Budget Statistics</a></li>
                <li class="layui-nav-item"><a href="/other/"><i class="layui-icon layui-icon-time"></i>Stock Records</a></li>
                <li class="layui-nav-item"><a href="/notifications/"><i class="layui-icon layui-icon-notice"></i>Notifications</a></li>
                {% if session.get('role') == 'super_admin' %}
                <li class="layui-nav-item"><a href="/admin/manage_users"><i class="layui-icon layui-icon-set"></i>User Management</a></li>
                {% endif %}
            </ul>
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item">
                    <a href="/logout" class="layui-btn layui-btn-danger">Logout</a>
                </li>
            </ul>
        </div>

        <div style="padding: 5%;">
            <h2>User Management</h2>
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>User Name</th>
                        <th>Edit</th>
                    </tr>
                </thead>
                <tbody>
                    {% for manager in stock_managers %}
                    <tr>
                        <td>{{ manager }}</td>
                        <td>
                            <button class="layui-btn layui-btn-xs" onclick="showChangePasswordModal('{{ manager }}')">Change password</button>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <div id="changePasswordModal" class="layui-layer" style="display: none;">
        <form class="layui-form" id="changePasswordForm">
            <input type="hidden" id="changeUsername" name="username">
            <div class="layui-form-item">
                <label class="layui-form-label">New Password</label>
                <div class="layui-input-inline">
                    <input type="password" id="newPassword" name="new_password" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="changePasswordSubmit">Submit</button>
                    <button type="button" class="layui-btn layui-btn-primary" onclick="hideChangePasswordModal()">取消</button>
                </div>
            </div>
        </form>
    </div>

    <script>
        layui.use('form', function () {
            var form = layui.form;

            form.on('submit(changePasswordSubmit)', function (data) {
                $.ajax({
                    url: '/admin/change_password',
                    type: 'POST',
                    data: data.field,
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg, { icon: 1 });
                            hideChangePasswordModal();
                        } else {
                            layer.msg(res.msg, { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('There was an error in the request. Please try again later', { icon: 2 });
                    }
                });
                return false;
            });
        });

        function showChangePasswordModal(username) {
            $('#changeUsername').val(username);
            layer.open({
                type: 1,
                title: '修改密码',
                content: $('#changePasswordModal'),
                area: ['400px', '250px']
            });
        }

        function hideChangePasswordModal() {
            layer.closeAll();
        }
    </script>
</body>
</html>